package com.hn.zhijian.system.rsasdk;


import org.apache.tomcat.util.codec.binary.StringUtils;

/**
 * AES加/解密工具类
 * 
 * @author shaohai.Liu
 * @date 2017-8-8
 * @version 0.0.1-SNAPSHOT
 */
public class AESUtils {
	/**
	 * 构造方法
	 */
	private AESUtils() {
		super();
	}

	/**
	 * RSA加密
	 * 
	 * @param keyBytes
	 *            密钥
	 * @param iv
	 *            向量
	 * @param plainText
	 *            明文
	 * @return 加密后的字符串
	 * @throws SecurityException
	 *             加解密错误
	 */
	public static String encrypt(final byte[] keyBytes, final byte[] iv, final byte[] plainText)
			throws SecurityException {
		AES aes = new AES();
		aes.setIv(iv);
		byte[] cipher = aes.encrypt(plainText, keyBytes);
		byte[] cipherCode = aes.encode(cipher);
		return StringUtils.newStringUtf8(cipherCode);
	}

	/**
	 * AES解密
	 * 
	 * @param keyBytes
	 *            密钥
	 * @param iv
	 *            向量
	 * @param cipherText
	 *            密文
	 * @return 解密后的字符串
	 * @throws SecurityException
	 *             加解密错误
	 */
	public static String decrypt(final byte[] keyBytes, final byte[] iv, final byte[] cipherText)
			throws SecurityException {
		AES aes = new AES();
		aes.setIv(iv);
		byte[] bytes = aes.decrypt(aes.decode(cipherText), keyBytes);
		return StringUtils.newStringUtf8(bytes);
	}
}
